package device

import (
	"context"

	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/svc"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/utils/dberrorhandler"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/types/pancakebot"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateDeviceLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateDeviceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateDeviceLogic {
	return &UpdateDeviceLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateDeviceLogic) UpdateDevice(in *pancakebot.DeviceInfo) (*pancakebot.BaseResp, error) {
	query := l.svcCtx.DB.Device.UpdateOneID(*in.Id).
		SetNotNilDeviceNo(pointy.GetPointer(int(*in.DeviceNo))).
		SetNotNilDeviceName(in.DeviceName).
		SetNotNilDeviceStatus(pointy.GetPointer(int(*in.DeviceStatus))).
		SetNotNilAddress(in.Address).
		SetNotNilExt(in.Ext).
		SetNotNilType(pointy.GetPointer(int(*in.Type)))

	err := query.Exec(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
	}
	return &pancakebot.BaseResp{Msg: errormsg.UpdateSuccess}, nil
}
